import { List } from "@nutui/nutui-taro";
import { FileItem } from "@nutui/nutui-taro/dist/types/__VUE/uploader/type";
import useSendGoodsStore from "../../store/sendGoods";
const sendGoodsStore = useSendGoodsStore();
export function reset() {
    // sendGoodsStore.sendAddressInfo = null     //发货地址
    // sendGoodsStore.getAddressInfo = null      //接货地址
    // sendGoodsStore.sendBranchPromise = null
    // sendGoodsStore.getBranchPromise = null
    //货物信息
    sendGoodsStore.goodsInfo = [
    ]
    //货物总重量
    sendGoodsStore.weigh = 0
    //货物总体积
    sendGoodsStore.goodsVolume = 0
    //货物总件数
    sendGoodsStore.goodsNumber = 0
    // 货物页面  null为新增   nubmer为修改时候货物的下标
    sendGoodsStore.goodsAddOrEditIndex = null
    // 付款方式
    sendGoodsStore.clearingForm = '1'
    //签单信息
    sendGoodsStore.clearingFormInfo = {
        contractedNo: '',
        customerName: '',
        companyName: ""
    }
    //是否接货
    sendGoodsStore.receivingStatus = '0'
    //接货时间段
    sendGoodsStore.receivingTime = {
        start: "",
        end: ""
    }
    // 送货方式
    sendGoodsStore.pickUpMode = '2'
    //送货时间段
    sendGoodsStore.deliveryTime = {
        start: "",
        end: ""
    }
    //送货类型
    sendGoodsStore.deliveryType = '0'
    //是否卸货
    sendGoodsStore.dischargeCargo = '0'
    //卸货中的货物类型
    sendGoodsStore.dischargeServer = '1'
    //楼梯电梯
    sendGoodsStore.dischargeElevator = '2'
    //楼层数
    sendGoodsStore.elevatorNum = '1'
    //是否加急
    sendGoodsStore.jj = '0'
    //承运类型
    sendGoodsStore.sendType = '2'
    //保价金额
    sendGoodsStore.insuredMoney = '0'
    //保价费
    sendGoodsStore.insuredFee = '0'
    //回单类型
    sendGoodsStore.billServer = ''
    //签几联
    sendGoodsStore.billNum = ''
    //是否代收货款
    sendGoodsStore.collectionPayment = '0'
    //代收货款金额
    sendGoodsStore.collectionMoney = '0'
    //返回周期
    sendGoodsStore.returnCycle = '1'
    //代收服务费
    sendGoodsStore.serveFee = '0'
    //返还方式
    sendGoodsStore.returnType = '2'
    //银行卡号
    sendGoodsStore.returunAccount = null
    //是否包装
    sendGoodsStore.packing = '0'
    // 发货网点
    sendGoodsStore.sendBranch = {
        pending: false,
        info: {
            AREA: '',
            /**
             * 市
             */
            CITY: '',
            /**
             * 网点名字
             */
            COMPANYNAME: '',
            /**
             * 维度
             */
            LAT: '',
            /**
             * 经度
             */
            LNG: '',
            /**
             * 全部地址
             */
            LOCAL: '',
            /**
             * 省
             */
            PROVINCE: '',
            /**
             * 网点编号
             */
            branchno: '',
            /**
             * 距离 单位是米
             */
            distance: 0
        }
    }
    // 收货网点
    sendGoodsStore.getBranch = {
        pending: false,
        info: {
            AREA: '',
            /**
             * 市
             */
            CITY: '',
            /**
             * 网点名字
             */
            COMPANYNAME: '',
            /**
             * 维度
             */
            LAT: '',
            /**
             * 经度
             */
            LNG: '',
            /**
             * 全部地址
             */
            LOCAL: '',
            /**
             * 省
             */
            PROVINCE: '',
            /**
             * 网点编号
             */
            branchno: '',
            /**
             * 距离 单位是米
             */
            distance: 0
        }
    }
    sendGoodsStore.costList = [
        {
            name: "工本费",
            m: '2'
        },
        {
            name: "送货费",
            m: '0'
        },
        {
            name: "接货费",
            m: '0'
        },
        {
            name: "长途运费",
            m: '0'
        },
        {
            name: "服务费",
            m: '0'
        },
        {
            name: "保价费",
            m: '0'
        },
        {
            name: "附单据费用",
            m: '0'
        },
        {
            name: "目的地带搬运卸货费",
            m: '0'
        }
    ]
}
export function getChangeData(item: any) {
    // sendGoodsStore.sendAddressInfo = null     //发货地址
    // sendGoodsStore.getAddressInfo = null      //接货地址
    // sendGoodsStore.sendBranchPromise = null
    // sendGoodsStore.getBranchPromise = null
    //列表返回的改单原始数据
    sendGoodsStore.changeList = item;
    sendGoodsStore.getAddressInfo = null;
    sendGoodsStore.sendAddressInfo = null;
    //货物信息
    sendGoodsStore.goodsInfo = goodsListComputed(item.goodsInfo || []);
    //货物总重量
    sendGoodsStore.weigh = goodsComputed(item.goodsInfo||[], 'weigh')
    //货物总体积
    sendGoodsStore.goodsVolume = goodsComputed(item.goodsInfo||[], 'goodsVolume')
    //货物总件数
    sendGoodsStore.goodsNumber = goodsComputed(item.goodsInfo||[], 'goodsNumber')
    // 货物页面  null为新增   nubmer为修改时候货物的下标
    sendGoodsStore.goodsAddOrEditIndex = null
    // 付款方式
    sendGoodsStore.clearingForm = item.clearingForm
    //签单信息
    sendGoodsStore.clearingFormInfo = {
        contractedNo: item.billCompany,
        customerName: '',
        companyName: "",
        demand: {},
    }
    //是否接货
    sendGoodsStore.receivingStatus = item.isReceiving
    //接货时间段
    sendGoodsStore.receivingTime = receivingTimeFilter(item.receivingTime)
    // 送货方式
    // sendGoodsStore.pickUpMode = item.pickUpMode == "自提"
    //     ? "1"
    //     : item.pickUpMode == "送货"
    //         ? "2"
    //         : "3"
    sendGoodsStore.pickUpMode = item.pickUpMode
    //送货时间段
    sendGoodsStore.deliveryTime = receivingTimeFilter(item.deliveryTime)
    //送货类型
    sendGoodsStore.deliveryType = item.deliveryType
    //是否卸货
    sendGoodsStore.dischargeCargo = item.dischargeCargo
    //卸货中的货物类型
    sendGoodsStore.dischargeServer = item.dischargeServer
    //楼梯电梯
    sendGoodsStore.dischargeElevator = item.dischargeElevator ? item.dischargeElevator : '0'
    //楼层数
    sendGoodsStore.elevatorNum = item.elevatorNum
    //是否加急
    sendGoodsStore.jj = item.urgent=='false'?'0':'1'
    //承运类型
    sendGoodsStore.sendType = item.sendType
    //保价金额
    sendGoodsStore.insuredMoney = item.insuredMoney
    //保价费
    sendGoodsStore.insuredFee = item.insuredFee
    //回单类型
    sendGoodsStore.billServer = item.billServer
    //签几联
    sendGoodsStore.billNum = item.billNum
    //是否代收货款
    sendGoodsStore.collectionPayment = item.collectionPayment
    //代收货款金额
    sendGoodsStore.collectionMoney = item.collectionMoney
    //返回周期
    sendGoodsStore.returnCycle = item.returnCycle
    //代收服务费
    sendGoodsStore.serveFee = item.serveFee
    //返还方式
    sendGoodsStore.returnType = item.returnType
    //银行卡号
    sendGoodsStore.returunAccount = item.returunAccount
    //是否包装
    sendGoodsStore.packing = item.packing=='1'?'1':'0'
    // 发货网点
    sendGoodsStore.sendBranch = {
        pending: false,
        info: {
            AREA: '',
            /**
             * 市
             */
            CITY: '',
            /**
             * 网点名字
             */
            COMPANYNAME: '',
            /**
             * 维度
             */
            LAT: '',
            /**
             * 经度
             */
            LNG: '',
            /**
             * 全部地址
             */
            LOCAL: '',
            /**
             * 省
             */
            PROVINCE: '',
            /**
             * 网点编号
             */
            branchno: '',
            /**
             * 距离 单位是米
             */
            distance: 0
        }
    }
    // 收货网点
    sendGoodsStore.getBranch = {
        pending: false,
        info: {
            AREA: '',
            /**
             * 市
             */
            CITY: '',
            /**
             * 网点名字
             */
            COMPANYNAME: '',
            /**
             * 维度
             */
            LAT: '',
            /**
             * 经度
             */
            LNG: '',
            /**
             * 全部地址
             */
            LOCAL: '',
            /**
             * 省
             */
            PROVINCE: '',
            /**
             * 网点编号
             */
            branchno: '',
            /**
             * 距离 单位是米
             */
            distance: 0
        }
    }
    sendGoodsStore.costList = [
        {
            name: "工本费",
            m: '2'
        },
        {
            name: "送货费",
            m: '0'
        },
        {
            name: "接货费",
            m: '0'
        },
        {
            name: "长途运费",
            m: '0'
        },
        {
            name: "服务费",
            m: '0'
        },
        {
            name: "保价费",
            m: '0'
        },
        {
            name: "附单据费用",
            m: '0'
        },
        {
            name: "目的地带搬运卸货费",
            m: '0'
        }
    ]
}
function goodsListComputed(list: Array<any>) {
    list.forEach((item: any) => {
        let aa = item.specifications ? item.specifications.split(';') : [];
        if(aa.length){
            aa = aa.slice(0,aa.length-1)
        }
        let arr: any = []
        aa.forEach((gg: any) => {
            gg = gg.replace(new RegExp("cm", "g"), '');
            let bb = gg.split('*');
            arr.push(
                {
                    h: bb[0],
                    w: bb[1],
                    l: bb[2],
                    sum: bb[3],
                })
        })
        item.specifications = arr
    })
    return list
}
function goodsComputed(list: Array<any>, name: string) {
    let weigh: number = 0
    let goodsVolume: number = 0
    let goodsNumber: number = 0
    list.forEach((item: any) => {
        weigh += item.weigh;
        goodsVolume += item.goodsVolume;
        goodsNumber += item.goodsNumber;
    })
    if (name == 'weigh') {
        return weigh
    } else if (name == 'goodsVolume') {
        return goodsVolume
    } else {
        return goodsNumber
    }
}
function receivingTimeFilter(item: string) {
    if (item) {
        let obj = {
            start: "",
            end: ""
        }
        let arr = item.split('&&');
        obj.start = arr[0];
        obj.end = arr[1];
        console.log(obj, 'obj');

        return obj
    } else {
        return {
            start: "",
            end: ""
        }
    }

}


